﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace LowLevelGraphics.ImageFormats.SVG
{
    /// <summary>
    /// This is a helper for a rounded rectangle
    /// </summary>
    public class RoundedRectangleRegion //EDIT: Regionshape hier gelöscht, da nicht benötigt.
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="RoundedRectangleRegion"/> class.
        /// </summary>
        public RoundedRectangleRegion()
        {
        }

        /// <summary>
        /// Gets the rounded rect.
        /// </summary>
        /// <param name="_baseRect">The _base rect.</param>
        /// <param name="_nRadius">The _n radius.</param>
        /// <returns></returns>
        public Region GetRoundedRect(RectangleF _baseRect, int _nRadius)
        {
            // If corner radius is less than or equal to zero, return the original rectangle
            if (_nRadius <= 0) { return new Region(_baseRect); }
            // If corner radius is greater than or equal to half the width or height (whichever is shorter) then
            //return a capsule instead of a lozenge.
            if (_nRadius >= (Math.Min(_baseRect.Width, _baseRect.Height) / 2.0))
                return GetCapsule(_baseRect);

            float fDiameter = _nRadius + _nRadius;
            RectangleF ArcRect = new RectangleF(_baseRect.Location, new SizeF(fDiameter, fDiameter));
            GraphicsPath RR = new GraphicsPath();
            // top left arc
            RR.AddArc(ArcRect, 180, 90);
            // top right arc
            ArcRect.X = _baseRect.Right - fDiameter;
            RR.AddArc(ArcRect, 270, 90);
            // bottom right arc
            ArcRect.Y = _baseRect.Bottom - fDiameter;
            RR.AddArc(ArcRect, 0, 90);
            // bottom left arc
            ArcRect.X = _baseRect.Left;
            RR.AddArc(ArcRect, 90, 90);
            RR.CloseFigure();

            return new Region(RR);
        }

        /// <summary>
        /// Gets the capsule.
        /// </summary>
        /// <param name="_baseRect">The _base rect.</param>
        /// <returns></returns>
        public Region GetCapsule(RectangleF _baseRect)
        {
            float fDiameter = 0.0f;
            RectangleF ArcRect = Rectangle.Empty;
            GraphicsPath RR = new GraphicsPath();

            try
            {
                if (_baseRect.Width > _baseRect.Height)
                {
                    //return Horizonal capsule
                    fDiameter = _baseRect.Height;
                    ArcRect = new RectangleF(_baseRect.Location, new SizeF(fDiameter, fDiameter));
                    RR.AddArc(ArcRect, 90, 180);
                    ArcRect.X = _baseRect.Right - fDiameter;
                    RR.AddArc(ArcRect, 270, 180);

                }
                else if (_baseRect.Height > _baseRect.Width)
                {
                    // Return vertical capsule
                    fDiameter = _baseRect.Width;
                    ArcRect = new RectangleF(_baseRect.Location, new SizeF(fDiameter, fDiameter));
                    RR.AddArc(ArcRect, 180, 180);
                    ArcRect.Y = _baseRect.Bottom - fDiameter;
                    RR.AddArc(ArcRect, 0, 180);
                }
                else
                {
                    //return circle
                    RR.AddEllipse(_baseRect);
                }
            }
            catch (Exception e)
            {
                string sLastError = e.Message;
                RR.AddEllipse(_baseRect);
            }
            finally
            {
                RR.CloseFigure();
            }
            return new Region(RR);
        }
    }
}
